﻿using System;
using System.Windows.Threading;
using JY.Wpf.Core.Tools;
using JY.Wpf.Core.ViewModelBases;

namespace JY.Wpf.TestForMetro.ViewModels.ControlDemoViewModels
{
    [NotifyPropertyChanged]
    public class ButtonsViewModel:ViewModelBase
    {
        #region # 变量
        private readonly DispatcherTimer _timer;
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public ButtonsViewModel()
        {
            _timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(200)
            };
            _timer.Tick += Timer_Tick;
        }
        #endregion

        #region # 属性

        public int Progress { get; set; }

        public bool IsUploading { get; set; }
        #endregion

        #region # Command方法
        #region # 进度按钮执行——void ProgressExceute()
        /// <summary>
        /// 进度按钮执行
        /// </summary>
        public void ProgressExceute()
        {
            if (_timer.IsEnabled)
            {
                _timer.Stop();
            }
            else
            {
                _timer.Start();
            }
        }
        #endregion
        #endregion

        #region # 方法

        private void Timer_Tick(object sender, EventArgs e)
        {
            this.Progress++;
            if (Progress == 100)
            {
                Progress = 0;
                _timer.Stop();
                IsUploading = false;
            }
        }
        #endregion

        #region # 获取页面 —— object GetView(object context = null)
        /// <summary>
        /// 获取页面
        /// </summary>
        /// <param name="context">页面</param>
        /// <returns></returns>
        public override object GetView(object context = null)
        {
            return ResolveMediator.Resolve<ButtonsView>();
        }
        #endregion
    }
}
